/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package VLSys;

import java.awt.*;

import javax.swing.*;
import DBConnection.*;

import java.sql.*;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;




import javax.swing.table.DefaultTableModel;


public class ReportsVehicles extends JPanel{
    JLabel serviceList;
    
    JScrollPane vehicleScroll;
    JTable vehicleTable;
    
    JTextField filter;
    JLabel filterLabel;
    
    DefaultTableModel vehicleTableModel;
    
        
    public ReportsVehicles(final JFrame frame)
    {   setVisible(false);
        setLayout(null);
        setBackground(null);
        
        serviceList = new JLabel("VEHICLES");
        serviceList.setFont(new Font("sansserif", Font.PLAIN, 40));
        serviceList.setBounds(10,10,300,30);
        
        filterLabel = new JLabel("Search:");
        filterLabel.setBounds(394,400,150,20);
        
        filter = new JTextField();
        filter.setBounds(439,400,150,20);
        filter.setBorder(null);
        
        //table codes
        Object headers[] = {"Vehicle Type ID", "Manufacturer", "Model", "Size"};
        Object dummyData[][] = {{"veh001", "Sarao", "ExpressCarabao100", "L"},
                            {"veh002", "Pilipinas Hino", "Bus Ranger", "L"}};
    
        vehicleTable = new JTable();
        vehicleTable.setModel(new javax.swing.table.DefaultTableModel(null, headers
        ) {


            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
        vehicleTableModel = (DefaultTableModel)vehicleTable.getModel();
        
        vehicleScroll = new JScrollPane();
        vehicleTable.getTableHeader().setReorderingAllowed(false);
        vehicleScroll.setViewportView(vehicleTable);
        vehicleScroll.setBounds(15, 50, 575, 345);
        vehicleTable.getTableHeader().setResizingAllowed(false);
        
        add(serviceList);
        add(vehicleScroll);
        add(filter);
        add(filterLabel);
        
        filter.addKeyListener(new KeyAdapter() {
          public void keyReleased(KeyEvent e) {
              
              if (e.getKeyCode()==39 || e.getKeyCode()==37)
        {
               
        }
              else 
                filter.setText(capitalizeFirstLetters(filter.getText()));
     
          
          
          populateTable("SELECT * FROM vehiclelist WHERE model LIKE '" + filter.getText() + "%' ORDER BY model ASC");
          
          }

          public void keyTyped(KeyEvent e) {
          }

          public void keyPressed(KeyEvent e) {
          }
        });
        
        
    }
    
    
    public void populateTable(String pS){
         
              try{
                    //opens db connection
                    ConnectionFactory myFactory = ConnectionFactory.getFactory();
                    Connection conn = myFactory.getConnection();

                    //SQL Query
                    PreparedStatement pstmt = conn.prepareStatement(pS);

                    //execute query
                    ResultSet rs = pstmt.executeQuery();
                    
                    vehicleTableModel.getDataVector().removeAllElements();
                    vehicleTable.setAutoCreateRowSorter(true);

                    
                    //populate JTable 
                    while(rs.next()){
                    vehicleTableModel.addRow(new Object[]{rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4)});
                    vehicleTableModel.fireTableDataChanged();
                    }
                    
                    //close DB connection
                    conn.close();
                }
                      
                catch(SQLException e)
                {
                   e.printStackTrace();
                   System.out.print("ayaw!");
                }
             
          }
    
    public static String capitalizeFirstLetters ( String s ) {

        for (int i = 0; i < s.length(); i++) {

            if (i == 0) {
                // Capitalize the first letter of the string.
                s = String.format( "%s%s",
                             Character.toUpperCase(s.charAt(0)),
                             s.substring(1) );
            }

            // Is this character a non-letter or non-digit?  If so
            // then this is probably a word boundary so let's capitalize
            // the next character in the sequence.
            if (!Character.isLetterOrDigit(s.charAt(i))) {
                if (i + 1 < s.length()) {
                    s = String.format( "%s%s%s",
                                 s.subSequence(0, i+1),
                                 Character.toUpperCase(s.charAt(i + 1)),
                                 s.substring(i+2) );
                }
            }

        }

        return s;

    }
    
       
    
    
    
}
